#include "MainMenu.h"
#include "TCPClient.h"
#include "ui_MainMenu.h"
#include <QAction>
#include<QDebug>
MainMenu::MainMenu(QRect screenGeometry,QString phone,QString pass,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainMenu)
{
    ui->setupUi(this);
    MainMenu::phone=phone;
    MainMenu::pass=pass;
    this->screenGeometry=screenGeometry;
    this->setFixedSize(this->screenGeometry.width(),this->screenGeometry.height());
    qDebug()<<"mainMenu "<<this->screenGeometry.width()<<"   "<<this->screenGeometry.height()<<endl;
    for(int i=0;i<rows*column;i++)
    {
        QString styleSheet = QString("QPushButton{border:none;background-color:rgba(255, 255, 255,100%);}");
        functions.append(new QPushButton);
        functions.last()->setFixedSize(this->screenGeometry.width()/2-50,this->screenGeometry.width()/2-50);
        functions.last()->setStyleSheet(styleSheet);
    }

    QString styleSheet = QString("QPushButton{border-image: url(%1);color:red;font-size:40px;font-weight:bold;}");
    functions[0]->setStyleSheet(styleSheet.arg(":/source/line.png"));
    functions[0]->setText("排队取号");
    connect(functions[0],&QPushButton::clicked,[=](){
            packMessageToServer(2);
        });
    functions[1]->setStyleSheet(styleSheet.arg(":/source/information.png"));
    functions[1]->setText("个人信息");
    connect(functions[1],&QPushButton::clicked,[=](){
             packMessageToServer(4);
        });

    pLayout = new QGridLayout();
    init();
}

MainMenu::~MainMenu()
{
    delete ui;
}

void MainMenu::creatRegistration(QString data)
{
    this->registration=new Registration(this->screenGeometry,data);
    connect(this->registration,&Registration::toServerSend_signals,this,&MainMenu::toServerSend_signals);
    connect(this,&MainMenu::receiveServerData_signal,this->registration,&Registration::receiveServer_slot);
}

void MainMenu::creatPersonal(QString phone, QString id, QString name, QString nation, QString sex, QString blood, QString birth, QString illness, QString irritability)
{qDebug()<<"birth:"<<birth<<endl;
    if(this->personal==nullptr)
    {
        this->personal=new Personal(this->screenGeometry,phone,id,name,nation,sex,blood,birth,illness,irritability);
        connect(this->personal,&Personal::toServerSend_signals,this,&MainMenu::toServerSend_signals);
        this->personal->show();
    }
    else
        this->personal->showPersonal(phone,id,name,nation,sex,blood,birth,illness,irritability);
}


void MainMenu::init()
{
    for(int r=0;r<rows;r++)
        for(int c=0;c<column;c++)
        {
            pLayout->addWidget(functions[r*column+c],r,c);
        }
    this->setLayout(pLayout);

    connect(functions[0],&QPushButton::clicked,this,[=](){

    });
}

void MainMenu::packMessageToServer(int function)
{qDebug()<<"进入packMessageToServer"<<endl;
    QString str("7E7E;kind:phone;function:%1;");
    str.append("phone:"+this->phone+";");
    str.append("pass:"+this->pass+";");
    if(function==2)//进入排队页面
    {
        str=str.arg("02");
    }
    else if(function==4)
        str=str.arg("04");
    emit this->toServerSend_signals(str);
}

void MainMenu::receiveServerData_slot(int function, QMap<QString, QString> kv)
{
    switch (function) {
    case 2:
        creatRegistration(kv.value("medicalKind"));
    break;
    case 3:
        emit this->receiveServerData_signal(function,kv.value("choose"));
    break;
    case 4://pass:123456;name:caojinpeng;pass:123456;sex:0;id:360481199905260432;blood:O;
        creatPersonal(kv.value("phone"),kv.value("id"),kv.value("name"),kv.value("nation"),
                      kv.value("sex"),kv.value("blood"),kv.value("birth"),kv.value("illness"),kv.value("irritability"));
    break;
    case 5:
        this->personal->changePersonal(kv.value("status")=="true"?true:false);//更新信息
    break;
    case 7:
        //允许保存信息
        if(this->personal!=nullptr)
            this->personal->sendChangeInformation();
    break;
    }
}

QString MainMenu::phone;
QString MainMenu::pass;
